package com.zj.weather.base;

import android.content.Context;
import android.graphics.Canvas;

import com.zj.weather.utils.DisplayUtils;

import java.util.ArrayList;
import java.util.List;


@SuppressWarnings("unused")
public abstract class BaseWeatherBgView {

    protected Context context;
    protected int width, height;
    protected List<HoldersInfo> holders;

    public BaseWeatherBgView(Context context) {
        this.context = context;
    }

    protected abstract List<HoldersInfo> getHolders();

    private void initData() {
        if (holders != null)
            for (HoldersInfo info : holders) {
                info.initHolders(context);
            }
    }

    public void draw(Canvas canvas, float alpha) {
        if (holders != null)
            if (drawWeather(canvas, alpha)) {
                for (HoldersInfo info : holders) {
                    info.upDateHoldersValue(context, canvas, alpha);
                }
            }
    }

    public boolean drawWeather(Canvas canvas, float alpha) {
        return true;
    }

    public void setSize(int width, int height) {
        boolean isSameWidth = this.width == width;
        boolean isSameHeight = this.height == height;
        if (!isSameWidth || !isSameHeight) {
            this.width = width;
            this.height = height;
            setHolders();
        }
    }

    int convertAlphaColor(float percent, final int originalColor) {
        int newAlpha = (int) (percent * 255) & 0xFF;
        return (newAlpha << 24) | (originalColor & 0xFFFFFF);
    }

    private void setHolders() {
        holders = getHolders();
        initData();
    }

    protected int[] getDisplaySize() {
        return DisplayUtils.getDefaultDisplayMetrics(context);
    }

    public float dp2px(float value) {
        return DisplayUtils.dip2px(context, value);
    }
}
